package org.company.mars.storm;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.thrift7.TException;

import backtype.storm.Config;
import backtype.storm.LocalCluster;
import backtype.storm.LocalDRPC;
import backtype.storm.StormSubmitter;
import backtype.storm.coordination.BatchOutputCollector;
import backtype.storm.drpc.LinearDRPCTopologyBuilder;
import backtype.storm.generated.AlreadyAliveException;
import backtype.storm.generated.DRPCExecutionException;
import backtype.storm.generated.InvalidTopologyException;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.BasicOutputCollector;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseBasicBolt;
import backtype.storm.topology.base.BaseBatchBolt;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Tuple;
import backtype.storm.tuple.Values;
import backtype.storm.utils.DRPCClient;

public class ReachTopology {

    @SuppressWarnings("serial")
    public static Map<String, List<String>> TWEETERS_DB = new HashMap<String, List<String>>() {
        {
            put("foo.com/blog/1",
                    Arrays.asList("sally", "bob", "tim", "george", "nathan"));
            put("engineering.twitter.com/blog/5",
                    Arrays.asList("adam", "david", "sally", "nathan"));
            put("tech.backtype.com/blog/123",
                    Arrays.asList("tim", "mike", "john"));
        }
    };

    @SuppressWarnings("serial")
    public static Map<String, List<String>> FOLLOWERS_DB = new HashMap<String, List<String>>() {
        {
            put("sally", Arrays.asList("bob", "tim", "alice", "adam", "jim",
                    "chris", "jai"));
            put("bob", Arrays.asList("sally", "nathan", "jim", "mary", "david",
                    "vivian"));
            put("tim", Arrays.asList("alex"));
            put("nathan", Arrays.asList("sally", "bob", "adam", "harry",
                    "chris", "vivian", "emily", "jordan"));
            put("adam", Arrays.asList("david", "carissa"));
            put("mike", Arrays.asList("john", "bob"));
            put("john", Arrays.asList("alice", "nathan", "jim", "mike", "bob"));
        }
    };
    
    @SuppressWarnings("serial")
    public static class GetTweeters extends BaseBasicBolt{

        @Override
        public void execute(Tuple input, BasicOutputCollector collector) {
            Object id = input.getValue(0);
            
            String url = input.getString(1);
            List<String> tweeters = TWEETERS_DB.get(url);
            if(tweeters != null) {
                for(String tweeter : tweeters) {
                    collector.emit(new Values(id,tweeter));
                }
            }
        }

        @Override
        public void declareOutputFields(OutputFieldsDeclarer declarer) {
            declarer.declare(new Fields("id","tweeter"));
        }
    }
    
    @SuppressWarnings("serial")
    public static class GetFollowers extends BaseBasicBolt{

        @Override
        public void execute(Tuple input, BasicOutputCollector collector) {
            Object id = input.getValue(0);
            String tweeter = input.getString(1);
            
            List<String> followers = FOLLOWERS_DB.get(tweeter);
            if(followers != null) {
                for(String follower : followers) {
                    collector.emit(new Values(id,follower));
                }
            }
        }

        @Override
        public void declareOutputFields(OutputFieldsDeclarer declarer) {
            declarer.declare(new Fields("id","follower"));
        }
    }
    
    
    @SuppressWarnings({ "serial", "rawtypes" })
    public static class PartialQuener extends BaseBatchBolt {
        
        private BatchOutputCollector _collector;
        
        private Object _id;
        
        private Set<String> followers = null;

        @Override
        public void prepare(Map conf, TopologyContext context,
                BatchOutputCollector collector, Object id) {
            this._collector = collector;
            this._id = id;
            this.followers = new HashSet<String>();
        }

        @Override
        public void execute(Tuple tuple) {
            followers.add(tuple.getString(1));
        }

        @Override
        public void finishBatch() {
            _collector.emit(new Values(_id,followers.size()));
        }

        @Override
        public void declareOutputFields(OutputFieldsDeclarer declarer) {
            declarer.declare(new Fields("id","partial-count"));
        }
    }
    
    @SuppressWarnings({ "serial", "rawtypes" })
    public static class CountAggregate extends BaseBatchBolt{
        
        private BatchOutputCollector _collector;
        
        private Object _id;
        
        private int _count = 0;

        @Override
        public void prepare(Map conf, TopologyContext context,
                BatchOutputCollector collector, Object id) {
            this._collector = collector;
            this._id = id;
        }

        @Override
        public void execute(Tuple tuple) {
            _count += tuple.getInteger(1);
        }

        @Override
        public void finishBatch() {
            this._collector.emit(new Values(_id,_count));
        }

        @Override
        public void declareOutputFields(OutputFieldsDeclarer declarer) {
            declarer.declare(new Fields("id","reach"));
        }
    }
    
    public static LinearDRPCTopologyBuilder constructor() {
        LinearDRPCTopologyBuilder builder = new LinearDRPCTopologyBuilder("reach");
        builder.addBolt(new GetTweeters(),4);
        builder.addBolt(new GetFollowers(),6).shuffleGrouping();
        builder.addBolt(new PartialQuener(),6).fieldsGrouping(new Fields("id","follower"));
        builder.addBolt(new CountAggregate(),10).fieldsGrouping(new Fields("id"));
        
        return builder;
    }
    
    public static void main(String[] args) throws AlreadyAliveException, InvalidTopologyException {
        LinearDRPCTopologyBuilder builder = constructor();
        Config config = new Config();
        if(args == null || args.length == 0) {
            config.setMaxTaskParallelism(3);
            LocalDRPC drpc = new LocalDRPC();
            LocalCluster cluster = new LocalCluster();
            
            cluster.submitTopology("reach-drpc", config, builder.createLocalTopology(drpc));
            String[] urlsToTry = new String[]{ "foo.com/blog/1", "engineering.twitter.com/blog/5", "notaurl.com" };
            for(String url : urlsToTry) {
                System.out.println("Reach of " + url + ":" + drpc.execute("reach", url));
            }
            cluster.shutdown();
            drpc.shutdown();
        } else {
            config.setNumWorkers(1);
            StormSubmitter.submitTopology(args[0], config, builder.createRemoteTopology());
        }
    }
    
    public static String rpc(String host, Integer port , String tweeter) throws TException, DRPCExecutionException {
        DRPCClient client = new DRPCClient(host, port);
        return client.execute("reach", tweeter);
    }
}
